package edu.cornell.cs4740.wsd.tools;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class QuoteFixer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String filename = args[0];
		String outputFilename = args[1];
		
		File f = new File(filename);
		Scanner s  = null;
		try {
			s = new Scanner(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		System.out.println("File opened");
		
		StringBuilder inputText = new StringBuilder();
		while(s.hasNext()) {
			
			String currentLine = s.nextLine();
			//System.out.println(currentLine);
			Pattern regex = Pattern.compile("(.*gloss=\")(.*)(\".*)");
			Matcher m = regex.matcher(currentLine);
			
			if(m.matches()) {
				String quotesToFix = m.group(2);
				quotesToFix = quotesToFix.replaceAll("\"", "&quot;");
				System.out.println(m.group(2) + " replaced with " + quotesToFix);
				inputText.append(m.group(1));
				inputText.append(quotesToFix);
				inputText.append(m.group(3) + "\n");
			} else {
				inputText.append(currentLine + "\n");
			}
		}
		
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(outputFilename));
			bw.write(inputText.toString());
			bw.flush();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	
	}

}
